package com.tuantuan.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuantuan.dao.CommenDao;
import com.tuantuan.model.Courselist;
import com.tuantuan.model.CourselistWeeks;
import com.tuantuan.service.CommenService;

@Service("CourseListService")
@Transactional
public class CourseListServiceImpl implements CommenService<Courselist>{

	@Resource(name="CourseListDao")
	private CommenDao<Courselist> courselistDao;
	/**
	 * 添加课程表资料
	 */
	@Override
	public String addEntity(Courselist entity) {
		// TODO Auto-generated method stub
		Courselist cl = new Courselist();
		CourselistWeeks clw = (CourselistWeeks)entity;
		cl.setPlace(clw.getPlace());
		cl.setCoId(clw.getCoId());
		cl.setClassId(clw.getClassId());
		cl.setTcId(clw.getTcId());
		cl.setBeginClass(clw.getBeginClass());
		cl.setEndClass(clw.getEndClass());
		cl.setDays(clw.getDays());
		StringBuffer sb = new StringBuffer();
		//判断是否有不合法课程表录入信息
		Courselist clUnique = courselistDao.queryBySql(cl, "courselist.queryUniqueCourselist");
		if(clw.getWeek1()!= null && clw.getWeek1().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("1") > -1){
				return "fail,1";
			}
			sb.append("1,");
		}
		if(clw.getWeek2()!= null && clw.getWeek2().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("2") > -1){
				return "fail,2";
			}
			sb.append("2,");
		}
		if(clw.getWeek3()!= null && clw.getWeek3().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("3") > -1){
				return "fail,3";
			}
			sb.append("3,");
		}
		if(clw.getWeek4()!= null && clw.getWeek4().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("4") > -1){
				return "fail,4";
			}
			sb.append("4,");
		}
		if(clw.getWeek5()!= null && clw.getWeek5().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("5") > -1){
				return "fail,5";
			}
			sb.append("5,");
		}
		if(clw.getWeek6()!= null && clw.getWeek6().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("6") > -1){
				return "fail,6";
			}
			sb.append("6,");
		}
		if(clw.getWeek7()!= null && clw.getWeek7().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("7") > -1){
				return "fail,7";
			}
			sb.append("7,");
		}
		if(clw.getWeek8()!= null && clw.getWeek8().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("8") > -1){
				return "fail,8";
			}
			sb.append("8,");
		}
		if(clw.getWeek9()!= null && clw.getWeek9().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("9") > -1){
				return "fail,9";
			}
			sb.append("9,");
		}
		if(clw.getWeek10()!= null && clw.getWeek10().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("10") > -1){
				return "fail,10";
			}
			sb.append("10,");
		}
		if(clw.getWeek11()!= null && clw.getWeek11().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("11") > -1){
				return "fail,11";
			}
			sb.append("11,");
		}
		if(clw.getWeek12()!= null && clw.getWeek12().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("12") > -1){
				return "fail,12";
			}
			sb.append("12,");
		}
		if(clw.getWeek13()!= null && clw.getWeek13().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("13") > -1){
				return "fail,13";
			}
			sb.append("13,");
		}
		if(clw.getWeek14()!= null && clw.getWeek14().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("14") > -1){
				return "fail,14";
			}
			sb.append("14,");
		}
		if(clw.getWeek15()!= null && clw.getWeek15().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("15") > -1){
				return "fail,15";
			}
			sb.append("15,");
		}
		if(clw.getWeek16()!= null && clw.getWeek16().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("16") > -1){
				return "fail,16";
			}
			sb.append("16,");
		}
		if(clw.getWeek17()!= null && clw.getWeek17().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("17") > -1){
				return "fail,17";
			}
			sb.append("17,");
		}
		if(clw.getWeek18()!= null && clw.getWeek18().equals("yes")){
			if(clUnique.getWeeks().indexOf("18") > -1){
				return "fail,18";
			}
			sb.append("18,");
		}
		if(clw.getWeek19()!= null && clw.getWeek19().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("19") > -1){
				return "fail,19";
			}
			sb.append("19,");
		}
		if(clw.getWeek20()!= null && clw.getWeek20().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("20") > -1){
				return "fail,20";
			}
			sb.append("20,");
		}
		if(clw.getWeek21()!= null && clw.getWeek21().equals("yes")){
			if(clUnique != null && clUnique.getWeeks().indexOf("21") > -1){
				return "fail,21";
			}
			sb.append("21,");
		}
		sb.deleteCharAt(sb.length()-1);
		cl.setWeeks(sb.toString());
		courselistDao.addEntity(cl);
		return "succ";
	}

	/**
	 * 删除课程资料
	 */
	@Override
	public String deleteEntity(int entityId) {
		// TODO Auto-generated method stub
		Courselist c = new Courselist();
		c.setClId(entityId);
		Courselist cl = courselistDao.queryById(c);
		if(cl != null){
			courselistDao.deleteEntity(cl);
			return "succ";
		}
		return "fail";
	}

	/**
	 * 更新课程表资料
	 */
	@Override
	public String updateEntity(Courselist entity) {
		// TODO Auto-generated method stub
		CourselistWeeks clw = (CourselistWeeks)entity;
		Courselist cl = new Courselist();
		cl.setClId(clw.getClId());
		cl.setPlace(clw.getPlace());
		cl.setCoId(clw.getCoId());
		cl.setClassId(clw.getClassId());
		cl.setTcId(clw.getTcId());
		cl.setBeginClass(clw.getBeginClass());
		cl.setEndClass(clw.getEndClass());
		cl.setDays(clw.getDays());
		StringBuffer sb = new StringBuffer();
		Courselist clUnique = courselistDao.queryBySql(cl, "courselist.queryUniqueCourselistById");
		if(clUnique == null){
			if(clw.getWeek1()!= null && clw.getWeek1().equals("yes")){
				sb.append("1,");
			}
			if(clw.getWeek2()!= null && clw.getWeek2().equals("yes")){
				sb.append("2,");
			}
			if(clw.getWeek3()!= null && clw.getWeek3().equals("yes")){
				sb.append("3,");
			}
			if(clw.getWeek4()!= null && clw.getWeek4().equals("yes")){
				sb.append("4,");
			}
			if(clw.getWeek5()!= null && clw.getWeek5().equals("yes")){
				sb.append("5,");
			}
			if(clw.getWeek6()!= null && clw.getWeek6().equals("yes")){
				sb.append("6,");
			}
			if(clw.getWeek7()!= null && clw.getWeek7().equals("yes")){
				sb.append("7,");
			}
			if(clw.getWeek8()!= null && clw.getWeek8().equals("yes")){
				sb.append("8,");
			}
			if(clw.getWeek9()!= null && clw.getWeek9().equals("yes")){
				sb.append("9,");
			}
			if(clw.getWeek10()!= null && clw.getWeek10().equals("yes")){
				sb.append("10,");
			}
			if(clw.getWeek11()!= null && clw.getWeek11().equals("yes")){
				sb.append("11,");
			}
			if(clw.getWeek12()!= null && clw.getWeek12().equals("yes")){
				sb.append("12,");
			}
			if(clw.getWeek13()!= null && clw.getWeek13().equals("yes")){
				sb.append("13,");
			}
			if(clw.getWeek14()!= null && clw.getWeek14().equals("yes")){
				sb.append("14,");
			}
			if(clw.getWeek15()!= null && clw.getWeek15().equals("yes")){
				sb.append("15,");
			}
			if(clw.getWeek16()!= null && clw.getWeek16().equals("yes")){
				sb.append("16,");
			}
			if(clw.getWeek17()!= null && clw.getWeek17().equals("yes")){
				sb.append("17,");
			}
			if(clw.getWeek18()!= null && clw.getWeek18().equals("yes")){
				sb.append("18,");
			}
			if(clw.getWeek19()!= null && clw.getWeek19().equals("yes")){
				sb.append("19,");
			}
			if(clw.getWeek20()!= null && clw.getWeek20().equals("yes")){
				sb.append("20,");
			}
			if(clw.getWeek21()!= null && clw.getWeek21().equals("yes")){
				sb.append("21,");
			}
			sb.deleteCharAt(sb.length()-1);
			cl.setWeeks(sb.toString());
			courselistDao.updateEntity(cl);
			return "succ";
		}else{
			if(clw.getWeek1()!= null && clw.getWeek1().equals("yes")){
				if( clUnique.getWeeks().indexOf("1") > -1){
					return "fail,1";
				}
				sb.append("1,");
			}
			if(clw.getWeek2()!= null && clw.getWeek2().equals("yes")){
				if( clUnique.getWeeks().indexOf("2") > -1){
					return "fail,2";
				}
				sb.append("2,");
			}
			if(clw.getWeek3()!= null && clw.getWeek3().equals("yes")){
				if( clUnique.getWeeks().indexOf("3") > -1){
					return "fail,3";
				}
				sb.append("3,");
			}
			if(clw.getWeek4()!= null && clw.getWeek4().equals("yes")){
				if( clUnique.getWeeks().indexOf("4") > -1){
					return "fail,4";
				}
				sb.append("4,");
			}
			if(clw.getWeek5()!= null && clw.getWeek5().equals("yes")){
				if( clUnique.getWeeks().indexOf("5") > -1){
					return "fail,5";
				}
				sb.append("5,");
			}
			if(clw.getWeek6()!= null && clw.getWeek6().equals("yes")){
				if( clUnique.getWeeks().indexOf("6") > -1){
					return "fail,6";
				}
				sb.append("6,");
			}
			if(clw.getWeek7()!= null && clw.getWeek7().equals("yes")){
				if( clUnique.getWeeks().indexOf("7") > -1){
					return "fail,7";
				}
				sb.append("7,");
			}
			if(clw.getWeek8()!= null && clw.getWeek8().equals("yes")){
				if( clUnique.getWeeks().indexOf("8") > -1){
					return "fail,8";
				}
				sb.append("8,");
			}
			if(clw.getWeek9()!= null && clw.getWeek9().equals("yes")){
				if( clUnique.getWeeks().indexOf("9") > -1){
					return "fail,9";
				}
				sb.append("9,");
			}
			if(clw.getWeek10()!= null && clw.getWeek10().equals("yes")){
				if( clUnique.getWeeks().indexOf("10") > -1){
					return "fail,10";
				}
				sb.append("10,");
			}
			if(clw.getWeek11()!= null && clw.getWeek11().equals("yes")){
				if( clUnique.getWeeks().indexOf("11") > -1){
					return "fail,11";
				}
				sb.append("11,");
			}
			if(clw.getWeek12()!= null && clw.getWeek12().equals("yes")){
				if( clUnique.getWeeks().indexOf("12") > -1){
					return "fail,12";
				}
				sb.append("12,");
			}
			if(clw.getWeek13()!= null && clw.getWeek13().equals("yes")){
				if( clUnique.getWeeks().indexOf("13") > -1){
					return "fail,13";
				}
				sb.append("13,");
			}
			if(clw.getWeek14()!= null && clw.getWeek14().equals("yes")){
				if( clUnique.getWeeks().indexOf("14") > -1){
					return "fail,14";
				}
				sb.append("14,");
			}
			if(clw.getWeek15()!= null && clw.getWeek15().equals("yes")){
				if( clUnique.getWeeks().indexOf("15") > -1){
					return "fail,15";
				}
				sb.append("15,");
			}
			if(clw.getWeek16()!= null && clw.getWeek16().equals("yes")){
				if( clUnique.getWeeks().indexOf("16") > -1){
					return "fail,16";
				}
				sb.append("16,");
			}
			if(clw.getWeek17()!= null && clw.getWeek17().equals("yes")){
				if( clUnique.getWeeks().indexOf("17") > -1){
					return "fail,17";
				}
				sb.append("17,");
			}
			if(clw.getWeek18()!= null && clw.getWeek18().equals("yes")){
				if(clUnique.getWeeks().indexOf("18") > -1){
					return "fail,18";
				}
				sb.append("18,");
			}
			if(clw.getWeek19()!= null && clw.getWeek19().equals("yes")){
				if( clUnique.getWeeks().indexOf("19") > -1){
					return "fail,19";
				}
				sb.append("19,");
			}
			if(clw.getWeek20()!= null && clw.getWeek20().equals("yes")){
				if( clUnique.getWeeks().indexOf("20") > -1){
					return "fail,20";
				}
				sb.append("20,");
			}
			if(clw.getWeek21()!= null && clw.getWeek21().equals("yes")){
				if( clUnique.getWeeks().indexOf("21") > -1){
					return "fail,21";
				}
				sb.append("21,");
			}
			sb.deleteCharAt(sb.length()-1);
			cl.setWeeks(sb.toString());
			courselistDao.updateEntity(cl);
			return "succ";
		}
	}

	public CommenDao<Courselist> getCourselistDao() {
		return courselistDao;
	}

	public void setCourselistDao(CommenDao<Courselist> courselistDao) {
		this.courselistDao = courselistDao;
	}

	
	
}
